import { get, post, put, del } from "./client";
import { BaseResponse, UserInfo, LoginRequest, MobileLoginRequest, SendCodeRequest, RegisterRequest, SiteInfo, SiteListRequest, UserCenterData } from "./types";
import { OrderItem, OrderDetailData } from "../types/order";
const SW = '/sw';

// 用户登录和认证相关 API
export const authApi = {
  // 登录相关
  // GET /app/login.html - 登录页面数据
  getLoginPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/login.html`);
  },

  // POST /app/login - 用户登录
  login: (payload: LoginRequest) => {
    return post<BaseResponse<UserInfo>>(`${SW}/app/login`, payload);
  },

  // POST /app/loginout - 用户登出
  logout: () => {
    return post<BaseResponse<any>>(`${SW}/app/loginout`);
  },

  // GET /app/index/scan/download.html - APP下载页面数据
  getDownloadPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/index/scan/download.html`);
  },

  // 手机登录相关
  // GET /app/mobile/login.html - 手机登录页面数据
  getMobileLoginPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/mobile/login.html`);
  },

  // POST /app/mobile/login - 手机号验证码登录
  mobileLogin: (payload: MobileLoginRequest) => {
    return post<BaseResponse<UserInfo>>(`${SW}/app/mobile/login`, payload, { useFormData: true });
  },

  // POST /app/mobile/login/send_code - 发送手机登录验证码
  sendMobileLoginCode: (payload: SendCodeRequest) => {
    return post<BaseResponse<any>>(`${SW}/app/mobile/login/send_code`, payload, { useFormData: true });
  },

  // GET /app/mobile/bonding.html - 手机绑定页面数据
  getMobileBondingPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/mobile/bonding.html`);
  },

  // POST /app/mobile/bonding - 绑定手机号
  bindMobile: (payload: MobileLoginRequest) => {
    return post<BaseResponse<any>>(`${SW}/app/mobile/bonding`, payload);
  },

  // POST /app/mobile/bonding/send_code - 发送绑定验证码
  sendBindingCode: (payload: SendCodeRequest) => {
    return post<BaseResponse<any>>(`${SW}/app/mobile/bonding/send_code`, payload);
  },

  // 用户注册相关
  // GET /app/user/register/mobile.html - 手机注册页面数据
  getMobileRegisterPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/user/register/mobile.html`);
  },

  // POST /app/user/register/mobile/sendcode - 发送手机注册验证码
  sendRegisterCode: (payload: SendCodeRequest) => {
    return post<BaseResponse<any>>(`${SW}/app/user/register/mobile/sendcode/V2`, payload);
  },

  // POST /app/user/register/mobile - 手机号注册
  mobileRegister: (payload: RegisterRequest) => {
    return post<BaseResponse<UserInfo>>(`${SW}/app/user/register/mobile`, payload);
  },

  // POST /app/user/invite/mobile/sendcode - 发送邀请注册验证码
  sendInviteRegisterCode: (payload: SendCodeRequest) => {
    return post<BaseResponse<any>>(`${SW}/app/user/invite/mobile/sendcode`, payload);
  },

  // POST /app/user/mobile/invite/register - 邀请注册
  inviteRegister: (payload: RegisterRequest) => {
    return post<BaseResponse<UserInfo>>(`${SW}/app/user/mobile/invite/register`, payload);
  },

  // GET /app/user/invite/success.html - 邀请成功页面数据
  getInviteSuccessPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/user/invite/success.html`);
  },
};

// 用户中心相关 API
export const userCenterApi = {
  // GET /app/ucenter/index.html - 用户中心首页数据
  getIndexPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/index.html`);
  },

  // GET /app/ucenter/index.html/V2 - 获取用户个人信息接口
  getUserCenterData: () => {
    return get<any>(`${SW}/app/ucenter/index.html/V2`);
  },

  // GET /app/wechatMini/ucenter/user - 获取用户信息（备用接口）
  getUserInfoBackup: () => {
    return get<BaseResponse<UserInfo>>(`${SW}/app/wechatMini/ucenter/user`);
  },

  // GET /app/ucenter/help.html - 帮助页面数据
  getHelpPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/help.html`);
  },

  // GET /app/ucenter/about_us.html - 关于我们页面数据
  getAboutUsPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/about_us.html`);
  },

  // GET /app/ucenter/how_use.html - 使用说明页面数据
  getHowUsePageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/how_use.html`);
  },

  // GET /app/ucenter/join_us.html - 加入我们页面数据
  getJoinUsPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/join_us.html`);
  },

  // GET /app/ucenter/my_wallet.html - 我的钱包页面数据
  getMyWalletPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/my_wallet.html`);
  },

  // GET /app/ucenter/privacy_policy.html - 隐私政策页面数据
  getPrivacyPolicyPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/privacy_policy.html`);
  },

  // GET /app/ucenter/user_agreement.html - 用户协议页面数据
  getUserAgreementPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/user_agreement.html`);
  },

  // GET /app/ucenter/user_info.html - 用户信息页面数据
  getUserInfoPageData: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/user_info.html`);
  },

  // GET /app/wechatMini/ucenter/user - 获取用户信息
  getUserInfo: () => {
    return get<BaseResponse<UserInfo>>(`${SW}/app/wechatMini/ucenter/user`);
  },

  // POST /app/ucenter/merchants_join - 商家加盟申请
  merchantsJoin: (payload: any) => {
    return post<BaseResponse<any>>(`${SW}/app/ucenter/merchants_join`, payload);
  },

  // POST /app/ucenter/saveBajiaoCardInfo - 保存银行卡信息
  saveBajiaoCardInfo: (payload: any) => {
    return post<BaseResponse<any>>(`${SW}/app/ucenter/saveBajiaoCardInfo`, payload);
  },

  // GET /app/ucenter/isPromptpay - 检查是否支持PromptPay
  isPromptpay: () => {
    return get<BaseResponse<any>>(`${SW}/app/ucenter/isPromptpay`);
  },
};

// 微信小程序相关 API
export const wechatMiniApi = {
  // GET /app/wechatMini/userCenter - 用户中心
  getUserCenter: () => {
    return get<BaseResponse<any>>(`${SW}/app/wechatMini/userCenter`);
  },

  // POST /app/wechatMini/supplementUserInfo - 完善用户信息
  supplementUserInfo: (payload: any) => {
    return post<BaseResponse<any>>(`${SW}/app/wechatMini/supplementUserInfo`, payload);
  },

  // GET /app/wechatMini/havePhone - 检查是否已绑定手机号
  havePhone: () => {
    return get<BaseResponse<any>>(`${SW}/app/wechatMini/havePhone`);
  },

  // POST /app/wechatMini/getPhoneNumber - 获取手机号
  getPhoneNumber: (payload: any) => {
    return post<BaseResponse<any>>(`${SW}/app/wechatMini/getPhoneNumber`, payload);
  },
};

// 网点相关 API
export const siteApi = {
  // GET /app/site/around/list - 获取附近网点列表
  getAroundSites: (params: SiteListRequest) => {
    return get<BaseResponse<SiteInfo[]>>(`${SW}/app/site/around/list/V2`, params);
  },

  // GET /app/site/detail.html/V2 - 获取网点详情
  getSiteDetail: (siteId: number, distance?: number) => {
    const params: any = { siteId };
    if (distance !== undefined) {
      params.distance = distance;
    }
    return get<BaseResponse<any>>(`${SW}/app/site/detail.html/V2`, params);
  },
};

// 订单相关 API
export const orderApi = {
  // POST /app/rentbox/order/list.html - 获取订单列表
  getOrderList: (orderStatus?: number) => {
    const body = orderStatus !== undefined ? { status: orderStatus } : {};
    return post<BaseResponse<OrderItem[]>>(`${SW}/app/rentbox/order/list.html/V2`, body);
  },
  
  // GET /app/rentbox/order/detail.html/V2 - 获取订单详情
  getOrderDetail: (orderId: number) => {
    return get<BaseResponse<OrderDetailData>>(`${SW}/app/rentbox/order/detail.html/V2?orderId=${orderId}`);
  },
};

export default {
  authApi,
  userCenterApi,
  wechatMiniApi,
  siteApi,
  orderApi,
};
